<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('notifications', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->enum('type', [
                'follow',           // 关注通知
                'comment',          // 评论通知
                'like',             // 点赞通知
                'favorite',         // 收藏通知
                'post_approved',    // 文章审核通过
                'post_rejected',    // 文章审核拒绝
                'mention',          // @提到
                'system'            // 系统通知
            ]);
            $table->morphs('notifiable');  // 关联的对象（文章、评论等）
            $table->foreignId('from_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->string('title');
            $table->text('content')->nullable();
            $table->json('data')->nullable();  // 额外数据
            $table->boolean('is_read')->default(false);
            $table->timestamp('read_at')->nullable();
            $table->timestamps();

            // 添加索引
            $table->index(['user_id', 'is_read']);
            $table->index(['user_id', 'created_at']);
            $table->index('type');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('notifications');
    }
};